package com.buba.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.buba.mapper.DeptMapper;
import com.buba.mapper.LgAreaMapper;
import com.buba.mapper.VendorMapper;
import com.buba.pojo.Dept;
import com.buba.pojo.LgArea;
import com.buba.pojo.Vendor;
import com.buba.service.VendorService;
import com.guli.common.utils.AliyunOSSUtil;
import com.guli.common.utils.IdWorker;
import com.mysql.cj.util.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 厂商表 服务实现类
 * </p>
 *
 * @author MrJia
 * @since 2020-10-29
 */
@Service
public class VendorServiceImpl extends ServiceImpl<VendorMapper, Vendor> implements VendorService {

    @Autowired
    private VendorMapper vendorMapper;
    @Autowired
    private LgAreaMapper lgAreaMapper;
    @Autowired
    private DeptMapper deptMapper;

//    @Override
//    @Transactional
//    public Boolean add(Vendor vendor, MultipartFile businessLicenseFile, MultipartFile signContentFile) {
//        try {
//            //oss 上传图片
//            String businessLicenseFileUrl = "/home/c/图片/" + UUID.randomUUID() + new Date(System.currentTimeMillis()) + ".jpg";
//            String signContentFileUrl = "/home/c/图片/" + UUID.randomUUID() + new Date(System.currentTimeMillis()) + ".jpg";
//            businessLicenseFile.transferTo(new File(businessLicenseFileUrl));
//            signContentFile.transferTo(new File(signContentFileUrl));
//            String upload = AliyunOSSUtil.upload(new File(businessLicenseFileUrl));
//            String upload1 = AliyunOSSUtil.upload(new File(signContentFileUrl));
//            vendor.setBusinessLicense(upload);
//            vendor.setSignContent(upload1);
//            //添加厂商
//            IdWorker idWorker = new IdWorker();
//            long id = idWorker.nextId();
//            vendor.setId(String.valueOf(id));
//            vendor.setReviewStatus(0);
//            System.out.println(vendor);
//            vendorMapper.insert(vendor);
//            return true;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return false;
//        }
//    }

    @Override
    public IPage<Vendor> selectList(Integer page, Integer limit, Vendor vendor) {
        //vendorName: '', //厂商名称 vendorType:'', //厂商类型 provinceId:"",//省id cityId:"",//市id countyId:"",//县id state:"",//状态：0:使用中/1:停用
        Page<Vendor> vendorPage = new Page<>(page, limit);
        QueryWrapper<Vendor> vendorQueryWrapper = new QueryWrapper<>();
        //厂商名称
        if (!StringUtils.isNullOrEmpty(vendor.getVendorName())) {
            vendorQueryWrapper.lambda().like(Vendor::getVendorName, vendor.getVendorName());
        }
        //厂商类型
        if (!StringUtils.isNullOrEmpty(vendor.getVendorType())) {
            vendorQueryWrapper.lambda().eq(Vendor::getVendorType, vendor.getVendorType());
        }
        //部门
        if (!StringUtils.isNullOrEmpty(vendor.getSignDept())) {
            vendorQueryWrapper.lambda().eq(Vendor::getSignDept, vendor.getSignDept());
        }
        //省id
        if (!StringUtils.isNullOrEmpty(vendor.getProvinceId())) {
            vendorQueryWrapper.lambda().eq(Vendor::getProvinceId, vendor.getProvinceId());
        }
        //市id
        if (!StringUtils.isNullOrEmpty(vendor.getCityId())) {
            vendorQueryWrapper.lambda().eq(Vendor::getCityId, vendor.getCityId());
        }
        //县id
        if (!StringUtils.isNullOrEmpty(vendor.getCountyId())) {
            vendorQueryWrapper.lambda().eq(Vendor::getCountyId, vendor.getCountyId());
        }
        //状态
        if (vendor.getState() != null) {
            vendorQueryWrapper.lambda().eq(Vendor::getState, vendor.getState());
        }

        LgArea province = lgAreaMapper.selectById(vendor.getProvinceId());
        LgArea city = lgAreaMapper.selectById(vendor.getCityId());
        LgArea country = lgAreaMapper.selectById(vendor.getCountyId());
        Dept dept = deptMapper.selectById(vendor.getSignDept());
        vendor.setProvinceId(province.getAreaName());
        vendor.setCityId(city.getAreaName());
        vendor.setCountyId(country.getAreaName());
        vendor.setSignDept(dept.getDeptName());

        return vendorMapper.selectPage(vendorPage, vendorQueryWrapper);
    }

    @Override
    public IPage<Vendor> selectListByCondition(Page<Vendor> page, Vendor vendor) {
        return vendorMapper.selectListByCondition(page, vendor);
    }

    @Override
    public boolean updateDept(Vendor vendor) {
        int i = vendorMapper.updateById(vendor);
        if (i > 0) return true;
        return false;
    }
}
